//
//  ViewController.m
//  15-UI-Wangyi
//
//  Created by Snow on 16/8/3.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "MyLayout.h"
#define kReuse @"cell"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, GXLabelWidth>
@property (nonatomic, strong) NSMutableArray *arrayOfTitle;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *arr = @[@"头条", @"精选", @"娱乐", @"体育", @"网易号", @"北京", @"视频", @"财经", @"科技", @"汽车", @"时尚", @"图片", @"直播", @"热点", @"跟帖", @"轻松一刻"];
    _arrayOfTitle = [NSMutableArray arrayWithArray:arr];
    MyLayout *layout = [[MyLayout alloc] init];
    layout.delegate = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50) collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor clearColor];
    [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kReuse];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfTitle.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kReuse forIndexPath:indexPath];
    cell.label.text = [_arrayOfTitle objectAtIndex:indexPath.row];
    NSLog(@"%@", cell.label.text);
//    cell.backgroundColor = [UIColor redColor];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [_arrayOfTitle objectAtIndex:indexPath.row];
    CGRect rect = [string boundingRectWithSize:CGSizeMake(0, 50) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return CGSizeMake(rect.size.width, 50);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout height:(CGFloat)height heightForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *string = [_arrayOfTitle objectAtIndex:indexPath.row];
    CGRect rect = [string boundingRectWithSize:CGSizeMake(0, 50) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return rect.size.width + 0.5;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
